package com.lingyue.commons.zlib;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

import java.io.*;
import java.util.zip.GZIPOutputStream;

public class CompressUtil {
    /**
     * 压缩文件成Gzip格式，Linux上可使用 压缩文件夹生成后缀名为".gz"的文件并下载
     *
     */
    public static void compressFolder(String folderPath, String targzipFilePath,String date, String targetFile) {
        System.out.println("source folder:"+folderPath);
        System.out.println("target folder:"+targetFile);
        File fs = new File(targzipFilePath + ".gz");
        boolean delete = fs.delete();
        File srcPath = new File(folderPath);
        int length = srcPath.listFiles().length;
        // 设定读入缓冲区尺寸
        byte[] buf = new byte[1024];
        File[] files = srcPath.listFiles();
        try {
            // 建立压缩文件输出流
            FileOutputStream fout = new FileOutputStream (targzipFilePath);
            // 建立tar压缩输出流
            TarArchiveOutputStream tout = new TarArchiveOutputStream (fout);
            for (int i = 0; i < length; i++) {
                String filename = srcPath.getPath () + File.separator + files[i].getName ();
                // 打开需压缩文件作为文件输入流
                // filename是文件全路径
                FileInputStream fin = new FileInputStream (filename);
                // 此处必须使用new
                TarArchiveEntry tarEn = new TarArchiveEntry (files[i]);
                // 此处需重置名称，默认是带全路径的，否则打包后会带全路径
                tarEn.setName (files[i].getName ());
                tout.putArchiveEntry (tarEn);
                int num;
                while ((num = fin.read (buf, 0, 1024)) != -1) {
                    tout.write (buf, 0, num);
                }
                tout.closeArchiveEntry ();
                fin.close ();
            }
            tout.close ();
            fout.close ();
            // 建立压缩文件输出流
            FileOutputStream gzFileStream = new FileOutputStream (targzipFilePath + ".gz");
            // 建立gzip压缩输出流
            GZIPOutputStream gzout = new GZIPOutputStream (gzFileStream);
            // 打开需压缩文件作为文件输入流
            // targzipFilePath是文件全路径
            FileInputStream tarin = new FileInputStream (targzipFilePath);
            int len;
            while ((len = tarin.read (buf, 0, 1024)) != -1) {
                gzout.write (buf, 0, len);
            }
            gzout.close ();
            gzFileStream.close ();
            tarin.close ();
            File f = new File (targzipFilePath);
            boolean del = f.delete ();
            File filesource = new File (targzipFilePath + ".gz");
            String targetPath = filesource.getAbsolutePath ();
            String targetNewFile = targetPath.replace (".YYCRMNOTSPEC.gz", ".YYCRMNOTSPEC");
            copyFile (targetPath, targetNewFile, true,date);
            //做备份并且移动到pix目录
            copyFile (targetNewFile, targetFile, false,date);
        } catch (FileNotFoundException e) {
            System.out.println (e);
        } catch (IOException e) {
            System.out.println (e);
        }
    }

    public static void copyFile(String source, String target, Boolean flag,String date) {
        try {
            System.out.println("copy "+source+"-->"+target + " flag:"+flag+ " date:"+date);
            File oldfile = new File(source);
            if (oldfile.exists()) {
                InputStream sourceSream = new FileInputStream(source);
                FileOutputStream targetStream = new FileOutputStream(target);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = sourceSream.read(buffer)) != -1) {
                    targetStream.write(buffer, 0, length);
                }
                targetStream.close();
                sourceSream.close();
            }
            if (flag) {
                oldfile.delete ();
            } else {
                System.out.println(source+" was renamed to "+source+"."+date);
                File bakFile=new File(source+"."+date);
                oldfile.renameTo (bakFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
